import os
import platform
import re
import pandas as pd
import numpy as np
from sklearn.datasets import load_iris

_ROOT = os.path.dirname(os.path.abspath(__file__))

def get_data(path):
    sysstr = platform.system()
    if (sysstr == 'Linux'):
        return os.path.join(_ROOT, 'docs/data', path)
    else:
        return os.path.join(_ROOT, 'docs\\data', path)


def load_meat():
    filename = get_data('meat.csv')
    df = pd.read_csv(filename, parse_dates=[0])
    return df


def load_births():
    filename = get_data('births_by_month.csv')
    df = pd.read_csv(filename, parse_dates=[0])
    return df


def load_vehicles():
    filename = get_data('vehicles_68000.csv')
    df = pd.read_csv(filename, dtype=str)
    return df


def demo_df(rowNum=100):
    pd.set_option('display.width', 1000)
    pd.set_option('colheader_justify', 'center')
    np.random.seed(6182018)
    demo_df_ = pd.DataFrame({'date': np.random.choice(pd.date_range('2018-01-01', '2018-06-18', freq='D'), rowNum),
                            'analysis_tool': np.random.choice(['pandas', 'r', 'julia', 'sas', 'stata', 'spss'],rowNum),
                            'database': np.random.choice(['postgres', 'mysql', 'sqlite', 'oracle', 'sql server', 'db2'],rowNum),
                            'os': np.random.choice(['windows 10', 'ubuntu', 'mac os', 'android', 'ios', 'windows 7', 'debian'],rowNum),
                            'num1': np.random.randn(rowNum)*100,
                            'num2': np.random.uniform(0,1,rowNum),
                            'num3': np.random.randint(100, size=rowNum),
                            'bool': np.random.choice([True, False], rowNum)
                           },
                           columns=['date', 'analysis_tool', 'num1', 'database', 'num2', 'os', 'num3', 'bool']
                )
    #print(demo_df_.head(20))
    return demo_df_


def load_iris_df():
    iris = load_iris()
    iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)
    iris_df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
    iris_df.columns = [re.sub("[() ]", "", col) for col in iris_df.columns]
    print(iris_df.head(20))
    return iris_df
